#include <Arduino.h>
// lib_deps = mathertel/OneButton@^2.6.1
#include "OneButtonTiny.h"  // This example also works with reduced OneButtonTiny class saving.

// 定义引脚编号
const int PIN_INPUT = 0;    // 按钮连接的GPIO引脚
const int PIN_LED = 3;       // LED正极连接的GPIO引脚

void myClickFunction() ;
void myDoubleClickFunction() ;
void myLongPressFunction() ;
// The actions I ca do...
typedef enum {
  ACTION_OFF,   // set LED "OFF".
  ACTION_ON,    // set LED "ON"
  ACTION_SLOW,  // blink LED "SLOW"
  ACTION_FAST   // blink LED "FAST"
} MyActions;

// Setup a new OneButton on pin PIN_INPUT.
// OneButton button(PIN_INPUT, true);
OneButtonTiny button(PIN_INPUT, true);  // This example also works with reduced OneButtonTiny class saving.

MyActions nextAction = ACTION_OFF;  // no action when starting


// setup code here, to run once.
void setup() {
  // enable the standard led on pin 13.
  pinMode(PIN_LED, OUTPUT);  // sets the digital pin as output

  // link the myClickFunction function to be called on a click event.
  button.attachClick(myClickFunction);

  // link the doubleclick function to be called on a doubleclick event.
  button.attachDoubleClick(myDoubleClickFunction);

  // link the doubleclick function to be called on a doubleclick event.
  button.attachLongPressStart(myDoubleClickFunction);

  // set 80 msec. debouncing time. Default is 50 msec.
  button.setDebounceMs(80);
}  // setup


// main code here, to run repeatedly:
void loop() {
  unsigned long now = millis();

  // keep watching the push button:
  button.tick();

  // You can implement other code in here or just wait a while

  if (nextAction == ACTION_OFF) {
    // do nothing.
    digitalWrite(PIN_LED, LOW);

  } else if (nextAction == ACTION_ON) {
    // turn LED on
    digitalWrite(PIN_LED, HIGH);

  } else if (nextAction == ACTION_SLOW) {
    // do a slow blinking
    if (now % 1000 < 500) {
      digitalWrite(PIN_LED, LOW);
    } else {
      digitalWrite(PIN_LED, HIGH);
    }  // if

  } else if (nextAction == ACTION_FAST) {
    // do a fast blinking
    if (now % 200 < 100) {
      digitalWrite(PIN_LED, LOW);
    } else {
      digitalWrite(PIN_LED, HIGH);
    }  // if
  }    // if
}  // loop


// this function will be called when the button was pressed 1 time and them some time has passed.
void myClickFunction() {
  if (nextAction == ACTION_OFF)
    nextAction = ACTION_ON;
  else
    nextAction = ACTION_OFF;
}  // myClickFunction


// this function will be called when the button was pressed 2 times in a short timeframe.
void myDoubleClickFunction() {
  if (nextAction == ACTION_ON) {
    nextAction = ACTION_SLOW;

  } else if (nextAction == ACTION_SLOW) {
    nextAction = ACTION_FAST;

  } else if (nextAction == ACTION_FAST) {
    nextAction = ACTION_ON;
  }  // if
}  // myDoubleClickFunction


// this function will be called when a long press was detected.
void myLongPressFunction() {
  nextAction = ACTION_OFF;
}  // myLongPressFunction
